<?php
//mysql 待操作适配类
class mysqladaptee implements target {
	
	protected $conn;   //用于存放数据库连接句柄
	//实现连接方法
	public function connect ($host,$user,$passwd,$dbname) {
		$conn = mysql_connet($host,$user,$passwd,$dbname);
		mysql_select_db($dbname,$conn);
		$this->$conn;
	}

	//查询方法
	public function query ($sql) {
		$res = mysql_query($sql,$this->$conn);
		return $res;
	}
	
	//关闭方法
	public function close () {
		mysql_close($this->$conn);
	}



}


//mysql操作适配类
class mysqladaptee {

	protected $conn;

	public function connect ($host,$user,$passwd,$dbname) {
		$conn = mysql_connet($host,$user,$passwd,$dbname);
		$this->$conn;	

	}
	
	public function query ($sql) {
		$res = mysql_query($sql,$this->$conn);
		return $res;
	}

	public function close () {
		mysql_close($this->$conn);
	}



}


//用户所期待的接口
interface target {

	public function connect($host,$user,$passwd,$dbname);

	public function query($sql);
	
	public function close();

}

//用户期待适配类
class database implements target {

	protected $db;

	public function __construct($type) {
		$type = $type.'adapter';
		$this->db = new $type;
	}

	public function connect($host,$user,$passwd,$dbname) {
		$this->db->connect($host,$user,$passwd,$dbname);
	}

	public function query($sql) {
		return $this->db->query($sql);
	}

	public function close () {
		$this->db->close();
	}

}

$db1 = new database('mysql');
$db1->connect('127.0.0.1','root','1234','mydb');
$db1->query('select * from test');
$db1->close();

$db2 = new database('mysqli');
$db2->connect('127.0.0.1','root','1234','mydb');
$db2->query('select * from test');
$db2->close();
